package com.etc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etc.domain.MockModel;

@Controller
public class Test1Controller {
    
    @RequestMapping("test.action")
    public ModelAndView aaa(@RequestParam("id") Integer id) {
        System.out.println(id);
        // 数据模型 和 视图
        ModelAndView mav = new ModelAndView();
        
        // TODO Mocke
        List<MockModel> mlist = new ArrayList();
        
        mlist.add(new MockModel(1, "中国"));
        mlist.add(new MockModel(2, "美国"));
        mlist.add(new MockModel(3, "英国"));
        mlist.add(new MockModel(4, "德国"));
        mlist.add(new MockModel(5, "法国"));
        mlist.add(new MockModel(6, "韩国"));
        
        mav.addObject("mlist", mlist); // request.setAttr
        mav.setViewName("test1"); // 前缀+test1+后缀
        return mav;
    }
    
    @RequestMapping("test2.action")
    @ResponseBody
    public List<MockModel> test2() {
        // TODO Mocke
        List<MockModel> mlist = new ArrayList();
        
        mlist.add(new MockModel(1, "中国"));
        mlist.add(new MockModel(2, "美国"));
        mlist.add(new MockModel(3, "英国"));
        mlist.add(new MockModel(4, "德国"));
        mlist.add(new MockModel(5, "法国"));
        mlist.add(new MockModel(6, "韩国"));
        return mlist;
    }
    
    
    @RequestMapping("/{id}/test3.action")
    @ResponseBody
    public List<MockModel> test2(@PathVariable("id") Integer id) {
        System.out.println(id);
        // TODO Mocke
        List<MockModel> mlist = new ArrayList();
        
        mlist.add(new MockModel(1, "中国"));
        mlist.add(new MockModel(2, "美国"));
        mlist.add(new MockModel(3, "英国"));
        mlist.add(new MockModel(4, "德国"));
        mlist.add(new MockModel(5, "法国"));
        mlist.add(new MockModel(6, "韩国"));
        return mlist;
    }
    @GetMapping("/test4.action")
    @ResponseBody()
    public String test4(@RequestBody MockModel mod) {
        System.out.println(mod.getId());
     
        return "search request";
    }
    @PutMapping("/test4.action")
    @ResponseBody()
    public String test5(@RequestBody MockModel mod) {
        System.out.println(mod.getId());
     
        return "update request";
    }
    
    @DeleteMapping("/test4.action")
    @ResponseBody()
    public String test6(@RequestBody MockModel mod, HttpServletResponse resp) {
        System.out.println(mod.getId());
        resp.setStatus(403);
        return "update request";
    }
}
